package com.atguigu.staservice.controller;

import com.atguigu.commonutils.R;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Author: GengKY
 * @Date: 2021/11/10 8:56
 */
@Api(description = "获取本机信息")
@RequestMapping("/staservice/sta")
@RestController
public class LocalInfoController {


    @GetMapping("/getLocalInfo")
    public R getLocalInfo(){

        Map<String,String> map=new HashMap<>();

        Runtime runtime = Runtime.getRuntime();
        Properties props =System.getProperties();


        //Java信息：Java版本、Java路径、JavaVM
        map.put("JavaVersion",props.getProperty("java.version"));
        map.put("JavaHome",props.getProperty("java.home"));
        map.put("JavaVM",props.getProperty("java.vm.name"));

        //VM信息：总内存，空闲内存，最大内存
        map.put("vmTotalMemory", runtime.totalMemory() / 1024 / 1024 +"M");
        map.put("vmFreeMemory", runtime.freeMemory() / 1024 / 1024 +"M");
        map.put("vmMaxMemory", runtime.maxMemory() / 1024 / 1024 +"M");

        //操作系统信息
        InetAddress addr;
        try {
            addr = InetAddress.getLocalHost();
            //IP地址
            String ip=addr.getHostAddress().toString(); //获取本机ip
            map.put("ipAddr",ip);
        }catch (Exception e){
            e.printStackTrace();
        }

        //操作系统
        map.put("osName",props.getProperty("os.name"));
        map.put("osVersion",props.getProperty("os.version"));


        //本机信息
        Map<String, String> commap = System.getenv();
        String userName = commap.get("USERNAME");// 获取用户名
        String computerName = commap.get("COMPUTERNAME");// 获取计算机名
        String userDomain = commap.get("USERDOMAIN");// 获取计算机域名
        map.put("userName",userName);
        map.put("computerName",computerName);
        map.put("userDomain",userDomain);
        map.put("cpuNum",String.valueOf(runtime.availableProcessors()));//CPU数量


        map.forEach((x,y)->{
            System.out.println("key="+x+", value="+y);
        });

        return R.ok().data("msg",map);

    }


}
